package com.sweet.fourtimeoa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sweet.fourtimeoa.mapper.RoomMapper;
import com.sweet.fourtimeoa.pojo.dto.PageDto;
import com.sweet.fourtimeoa.pojo.dto.RoomReserveDto;
import com.sweet.fourtimeoa.pojo.dto.RoomsQueryDto;
import com.sweet.fourtimeoa.pojo.entity.MeetingRooms;
import com.sweet.fourtimeoa.pojo.vo.PageVo;
import com.sweet.fourtimeoa.pojo.vo.RoomVo;
import com.sweet.fourtimeoa.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private  RoomMapper roomMapper;
    @Override
    public PageVo<MeetingRooms> getRoomByTime( Integer pageNum, Integer pageSize,Date startTime, Date endTime) {
        PageHelper.startPage(pageNum,pageSize);
      Page<MeetingRooms> rooms = (Page<MeetingRooms>) roomMapper.getRoomByTime(startTime,endTime);
      PageVo<MeetingRooms> pageVo = new PageVo<>();
        pageVo.setList(rooms.getResult());
        pageVo.setTotal(rooms.getTotal());
        pageVo.setPages(rooms.getPages());
        pageVo.setPageSize(rooms.getPageSize());
        pageVo.setPageNum(rooms.getPageNum());
      return pageVo;
    }

    @Override
    public void reserve(RoomReserveDto roomReserveDto) {
        for (Integer roomId : roomReserveDto.getRoomIds()) {
            roomMapper.reserve(roomId,roomReserveDto);
        }
    }

    @Override
    public PageVo<RoomVo> getRoomByUserId(PageDto<Integer> pageDto) {
        PageHelper.startPage(pageDto.getPageNum(),pageDto.getPageSize());
        Page<RoomVo> roomVos = (Page<RoomVo>) roomMapper.getRoomByUserId(pageDto.getData());
        PageVo<RoomVo> pageVo = new PageVo<>();
        pageVo.setList(roomVos.getResult());
        pageVo.setTotal(roomVos.getTotal());
        pageVo.setPages(roomVos.getPages());
        pageVo.setPageSize(roomVos.getPageSize());
        pageVo.setPageNum(roomVos.getPageNum());
        return pageVo;
    }

    @Override
    public void deleteRooms(List<Integer> updateIds) {
        roomMapper.deleteRooms(updateIds);
    }

    @Override
    public PageVo<RoomVo> getAllReserveRoom(PageDto<RoomsQueryDto> pageDto) {
        PageHelper.startPage(pageDto.getPageNum(),pageDto.getPageSize());
        Page<RoomVo> roomVos = (Page<RoomVo>) roomMapper.getAllReserveRoom(pageDto.getData());
        PageVo<RoomVo> pageVo = new PageVo<>();
        pageVo.setList(roomVos.getResult());
        pageVo.setTotal(roomVos.getTotal());
        pageVo.setPages(roomVos.getPages());
        pageVo.setPageSize(roomVos.getPageSize());
        pageVo.setPageNum(roomVos.getPageNum());
        return pageVo;
    }

    @Override
    public PageVo<MeetingRooms> getAllRoom(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPageNum(),pageDto.getPageSize());
        Page<MeetingRooms> rooms = (Page<MeetingRooms>)roomMapper.getAllRoom();
        PageVo<MeetingRooms> pageVo = new PageVo<>();
        pageVo.setList(rooms.getResult());
        pageVo.setTotal(rooms.getTotal());
        pageVo.setPages(rooms.getPages());
        pageVo.setPageSize(rooms.getPageSize());
        pageVo.setPageNum(rooms.getPageNum());
        return pageVo;
    }

    @Override
    public void deleteRoomById(Integer roomId) {
        roomMapper.deleteRoomById(roomId);
    }

    @Override
    public MeetingRooms getRoomById(Integer roomId) {
        return roomMapper.getRoomById(roomId);
    }

    @Override
    public void updateRoom(MeetingRooms meetingRooms) {
        roomMapper.updateRoom(meetingRooms);
    }

    @Override
    public void addRoom(MeetingRooms meetingRooms) {
        roomMapper.addRoom(meetingRooms);
    }
}
